import useAdminInfoStore from '@/store/useAdminInfo';
import { Navigate } from 'react-router-dom';

const AdminAuth = ({ children }: any) => {
  const { adminInfo } = useAdminInfoStore();

  // 逻辑反转：如果没有管理员信息，重定向到登录页
  if (!adminInfo?.id) {
    if (window.location.pathname === '/admin/login') {
      return children;
    }
    return <Navigate to="/admin/login" />;
  }

  // 如果访问的是登录页，则禁止访问
  if (window.location.pathname === '/admin/login') {
    return <Navigate to="/admin/home" />;
  }
  // 有管理员信息，允许访问子组件
  return children;
};

export default AdminAuth;
